package cn.danger.com.util;

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import cn.bmob.v3.datatype.BmobDate;

/**
 * Created by 俊 on 2016/12/31.
 */
public class DateUtil {

    public static String getDate(String mills) {
        String time = new BmobDate(new Date(Long.parseLong(mills))).getDate().toString();
        String[] date = time.split(" ")[0].split("-");
        return date[0] + "/" + date[1] + "/" + date[2];
    }

    public static String getDate(String year, String month, String dayOfMonth) {
        if (month.length() == 1) {
            month = "0" + month;
        }
        if (dayOfMonth.length() == 1) {
            dayOfMonth = "0" + dayOfMonth;
        }
        return year + "-" + month + "-" + dayOfMonth;
    }

    public static String getMonth(String month) {
        if (month.length() == 1) {
            month = "0" + month;
        }
        return month;
    }

    public static String getPreviousYearAndMonth(String year, String month) {
        String result = "";
        if (month.equals("1")) {
            result += (Integer.parseInt(year) - 1) + "-" + "12";
        } else {
            result = year + "-" + DateUtil.getMonth((Integer.parseInt(month) - 1) + "");
        }
        return result;
    }

    public static String getNextYearAndMonth(String year, String month) {
        String result = "";
        if (month.equals("12")) {
            result += (Integer.parseInt(year) + 1) + "-" + "01";
        } else {
            result = year + "-" + DateUtil.getMonth((Integer.parseInt(month) + 1) + "");
        }
        return result;
    }

    public static long getRemindTime(String s) {
        if (s.equals("准时")) {
            return 0;
        } else if (s.equals("5分钟前")) {
            return 60000 * 5;
        } else if (s.equals("15分钟前")) {
            return 60000 * 15;
        } else if (s.equals("30分钟前")) {
            return 60000 * 30;
        } else if (s.equals("1小时前")) {
            return 60000 * 60;
        } else {
            return 60000 * 120;
        }
    }

    public static long stringToLong(String strTime, String formatType)
            throws ParseException {
        Date date = stringToDate(strTime, formatType); // String类型转成date类型
        if (date == null) {
            return 0;
        } else {
            long currentTime = dateToLong(date); // date类型转成long类型
            return currentTime;
        }
    }

    public static Date longToDate(long currentTime, String formatType)
            throws ParseException {
        Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间
        String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
        Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型
        return date;
    }

    public static Date stringToDate(String strTime, String formatType)
            throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        date = formatter.parse(strTime);
        return date;
    }

    public static String dateToString(Date data, String formatType) {
        return new SimpleDateFormat(formatType).format(data);
    }

    public static long dateToLong(Date date) {
        return date.getTime();
    }

    /**
     * 使用用户格式提取字符串日期
     *
     * @param strDate 日期字符串
     * @param pattern 日期格式
     * @return
     */

    public static Date parse(String strDate, String pattern) {

        if (TextUtil.isEmpty(strDate)) {
            return null;
        }
        try {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.parse(strDate);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 使用用户格式格式化日期
     *
     * @param date    日期
     * @param pattern 日期格式
     * @return
     */

    public static String format(Date date, String pattern) {
        String returnValue = "";
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            returnValue = df.format(date);
        }
        return (returnValue);
    }

    /**
     * 通过年份和月份 得到当月的日子
     *
     * @param year
     * @param month
     * @return
     */
    public static int getMonthDays(int year, int month) {
        month++;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            case 2:
                if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
                    return 29;
                } else {
                    return 28;
                }
            default:
                return -1;
        }
    }

    /**
     * 返回当前月份1号位于周几
     *
     * @param year  年份
     * @param month 月份，传入系统获取的，不需要正常的
     * @return 日：1		一：2		二：3		三：4		四：5		五：6		六：7
     */
    public static int getFirstDayWeek(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, 1);
        Log.d("DateView", "DateView:First:" + calendar.getFirstDayOfWeek());
        return calendar.get(Calendar.DAY_OF_WEEK);
    }

}
